<template>
	<view class="home">
		<swiper class="swiper" circular autoplay>
			<swiper-item>
				<view class="bannerbox"></view>
			</swiper-item>
			<swiper-item>
				<view class="bannerbox"></view>
			</swiper-item>
			<swiper-item>
				<view class="bannerbox"></view>
			</swiper-item>
		</swiper>
		
		<view class="qrcodebox">
			<view class="box">
				<image src="../../static/svg/wifi.svg" class="icon"></image>
				<div class="titlebox">
					<div class="title">CMCC-sgsga</div>
					<div class="subTitle">一键连接 · 无需输入密码 · 安全上网</div>
				</div>
			</view>
		</view>

		<view class="home" v-if="isReconnect">
			<button class="home-but" @click="startWifi">重新连接</button>
		</view>
		<!-- 加载框-->
		<view v-if="isWifiLoading">
			<view class="loading-container">
				<image src="../../static/gif/wifi-load.gif" mode="aspectFit" class="wifi-logo"></image>
			</view>	
			<view>
				<div class="loadingTitle">正在连接WIFI...</div>
			</view>
		</view>
		
		<view class="home" v-if="isSuccess">
			<view class="loading-container">
				<image src="../../static/svg/success.svg" mode="aspectFit" class="wifi-logo"></image>
			</view>	
			<view>
				<div class="loadingTitle">WIFI 连接成功</div>
			</view>
		</view>
	</view>
</template>

<script>
	import { getWifiDetail } from '@/api/login/login'
	export default {
		data() {
			return {
				canIUseConnectWifi: false,
				isWifiLoading: false,
				isReconnect: false,
				isSuccess: false,
				wifiForm: {}
			}
		},
		onShow() {
		},
		onLoad(options) {
			// 自动加载
			console.log('页面参数:', options);
			const wfId = options.wfId;
			if (!wfId) {
				uni.showToast({
					title: 'WIFI信息错误',
					icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
					duration: 2000    //持续时间为 2秒
				})  
			} else {
				this.startWifi(wfId);
			}
		},
		methods: {
			// 位置信息获取
			authorizeLocation() {
				uni.authorize({
					scope: 'scope.userLocation',
					success: () => {
						// 用户授权成功后，开始连接WiFi
						this.startWifi();
					},
					fail: () => {
						uni.showToast({
							title: '位置信息授权失败',
							icon: 'none'
						});
					}
				});
			},
			// 开启WiFi模块
			startWifi(wfId) {
				// 获取WIFI基本信息
				let query = { wfId: wfId }
				getWifiDetail(query).then(res2 => {
					if (res2.code == '200') {
						this.wifiForm = res2.data
					} 
				});
				
				uni.startWifi({
					success: (success) => {
						console.log("success", success)
						this.detectionWifi();
					},
					fail: (err) => {
						console.log("开启WiFi模块失败", err);
						// 开启WiFi模块失败后的处理逻辑
					}
				});
			},
			// 检测当前是否链接该网络
			detectionWifi() {
				wx.getConnectedWifi({
					success: (res) => {
						console.log(res);
						if(res.wifi.SSID === 'dlbsx_3'){
							uni.showToast({
								title: '请勿重复连接'
							})
							return;
						}else{
							this.linkWifi();
						}
					},
					fail: (err) => {
						console.log(err);
						this.linkWifi();
					}
				})
			},
			// 连接wifi
			linkWifi(){
				this.isWifiLoading = true; // 显示加载动画
				this.isReconnect = false;
				this.isSuccess = false;
				uni.connectWifi({
					SSID: this.wifiForm.ssId,  //wifi名称
					//BSSID:'你的设备唯一值',//Wi-Fi 设备 BSSID 
					password: this.wifiForm.password, // 这里需要用户输入真实的WiFi密码
					success: () => {
						// console.log("WiFi连接成功");
						this.isWifiLoading = false // 关闭加载动画
						// 连接成功后的处理逻辑
						uni.showToast({
							title: 'WiFi连接成功'
						})
						this.isSuccess = true;
					},
					fail: (err) => {
						// console.log("WiFi连接失败", err);
						this.isWifiLoading = false // 关闭加载动画
						// 连接失败后的处理逻辑
						uni.showToast({
							title: 'WiFi连接失败,'
						})
						this.isReconnect = true;
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../static/style/mixin.scss';

	.home {
		width: 100vw;
		.swiper {
			height: 450upx;

			.bannerbox {
				width: 100vw;
				height: 450upx;
				background-image: linear-gradient(90deg, aqua, #f89);
			}
		}

		@keyframes move {
			from {
				right: -750upx;
			}

			to {
				right: 750upx;
			}

		}

		.qrcodebox {
			margin-top: 30upx;
			padding-left: 20upx;
			display: flex;

			.box {
				box-sizing: border-box;
				padding: 35upx;
				width: 800upx;
				height: 130upx;
				margin-right: 20upx;
				border-radius: 10upx;
				background-image: linear-gradient(90deg, aqua, #f89);
				display: flex;
				align-items: center;

				.icon {
					width: 80upx;
					height: 80upx;
					margin-right: 15upx;
				}

				.titlebox {
					margin-left: 20upx;
					.title {
						font-size: 35upx;
						margin-bottom: 18upx;
						font-weight: bold;
					}

					.subTitle {
						font-size: 20upx;
					}
				}
			}
		}
	}
	
	.home-but {
		width: 50%;
		height: 80rpx;
		margin-top: 100rpx;
		background-color: #1989FA;
		color: #FFFFFF;
		letter-spacing: 5rpx;
		line-height: 80rpx;
		outline: none;
		box-shadow: 2px 2px 5px #eeeeee;
	}
	
	.loading-container {
		display: flex;
		justify-content: center; /* 水平居中 */
		align-items: center;     /* 垂直居中 */
		height: 100%;  
		.wifi-logo {
			text-align: center;
		}
	}
	.loadingTitle {	
		text-align: center;
		font-size: 35upx;
		margin-bottom: 18upx;
		font-weight: bold;
	}
</style>
